package com.learning.platform.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    @Value("${upload.dir:./uploads}")
    private String uploadDir;
    
    @Value("${file.upload.path:uploads}")
    private String fileUploadPath;
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 确保上传目录存在
        File uploadDirectory = new File(uploadDir);
        if (!uploadDirectory.exists()) {
            uploadDirectory.mkdirs();
        }
        
        // 确保新的上传目录存在
        File fileUploadDirectory = new File(fileUploadPath);
        if (!fileUploadDirectory.exists()) {
            fileUploadDirectory.mkdirs();
        }
        
        // 映射 /uploads/** 到本地文件系统 (兼容旧版)
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:" + uploadDirectory.getAbsolutePath() + "/");

        // 映射 /static/files/** 到新的文件系统
        registry.addResourceHandler("/static/files/**")
                .addResourceLocations("file:" + fileUploadDirectory.getAbsolutePath() + "/");
    }
}





